package com.tapNhold.android.tipcalcu;

import java.text.DecimalFormat;

import com.tapNhold.android.tipcalcu.R;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TableRow;
import android.widget.ViewFlipper;

public class TipCalcuActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
	
	private static final int MENU_GET_PREFERENCES = Menu.FIRST;
	public static final DecimalFormat currencyFormat = new DecimalFormat("#0.00");

	private ViewFlipper viewFlipper;
	private float oldTouchValue;
	private TipCalcuComplete tipCalcuComplete;
	private TipCalcuSimple tipCalcuSimple;
	 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main1);
        
        viewFlipper = (ViewFlipper)findViewById(R.id.flipper);
        
        findViewById(R.id.buttonCaculate).setOnClickListener(this);
        findViewById(R.id.buttonCaculate2).setOnClickListener(this);
	    findViewById(R.id.buttonClear).setOnClickListener(this);
	    findViewById(R.id.buttonDone).setOnClickListener(this);	    
	    
	    findViewById(R.id.imageButtonTipAdd).setOnClickListener(this);
	    findViewById(R.id.imageButtonTipMinus).setOnClickListener(this);
	    findViewById(R.id.imageButtonTipAdd2).setOnClickListener(this);
	    findViewById(R.id.imageButtonTipMinus2).setOnClickListener(this);
	    findViewById(R.id.imageButtonDividedbyAdd).setOnClickListener(this);
	    findViewById(R.id.imageButtonDividedbyMinus).setOnClickListener(this);
	    findViewById(R.id.imageButtonEachpaysAdd).setOnClickListener(this);
	    findViewById(R.id.imageButtonEachpaysMinus).setOnClickListener(this);
	    
	    tipCalcuComplete = new TipCalcuComplete(this);
	    tipCalcuSimple = new TipCalcuSimple(this);
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent touchevent) {
    	
        switch (touchevent.getAction())
        {
	        case MotionEvent.ACTION_DOWN:
	        {
                oldTouchValue = touchevent.getX();
                break;
            }
	        
            case MotionEvent.ACTION_UP:
            {
                float currentX = touchevent.getX();
                if (oldTouchValue > currentX && viewFlipper.getDisplayedChild() == 0)
                {
    	        	viewFlipper.setInAnimation(TipCalcuAnimation.inFromRightAnimation());
    	        	viewFlipper.setOutAnimation(TipCalcuAnimation.outToLeftAnimation());
    	        	viewFlipper.showNext();  
    	        	tipCalcuSimple.calculate();
    	        	break;
                }
                if (oldTouchValue < currentX  && viewFlipper.getDisplayedChild() == 1)
                {
                	viewFlipper.setInAnimation(TipCalcuAnimation.inFromLeftAnimation());
                	viewFlipper.setOutAnimation(TipCalcuAnimation.outToRightAnimation());
                	viewFlipper.showPrevious(); 
                	tipCalcuComplete.calculate();
                	
                	break;
                }
                break;
            }
        }
        return false;
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	super.onCreateOptionsMenu(menu);
    	menu.add(0, MENU_GET_PREFERENCES, 0,
    			R.string.settings).setIcon(android.R.drawable.ic_menu_preferences);
    	
    	return true;
    }
    
    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
    	switch (item.getItemId()) {
    	case MENU_GET_PREFERENCES:
    		Intent intent = new Intent(TipCalcuActivity.this, TipCalcuPreferenceActivity.class);
            startActivity(intent);
    		return true;
    	}
    	return super.onMenuItemSelected(featureId, item);
    }
    
	@Override
	protected void onResume() {  
		super.onResume(); 
		
		SharedPreferences myPreference = PreferenceManager.getDefaultSharedPreferences(this);
		boolean bShowTaxPercent = myPreference.getBoolean("checkboxShowTaxPercent", false);
		boolean bShowTipPercent = myPreference.getBoolean("checkboxShowTipPercent", false);
		boolean bShowCouplepays = myPreference.getBoolean("checkboxShowCouplepays", false);

		TableRow trTaxPercent = (TableRow) this.findViewById(R.id.tableRowTax);
		TableRow trTipPercent = (TableRow) this.findViewById(R.id.tableRowTip);
		TableRow trCouplepays = (TableRow) this.findViewById(R.id.TableRowCouplepays);
		
		if (bShowTaxPercent) {
			trTaxPercent.setVisibility(View.VISIBLE);
		}
		else { 
			trTaxPercent.setVisibility(View.GONE);
		}
		
		if (bShowTipPercent) {
			trTipPercent.setVisibility(View.VISIBLE);
		}
		else { 
			trTipPercent.setVisibility(View.GONE);
		}
		
		if (bShowCouplepays) {
			trCouplepays.setVisibility(View.VISIBLE);
		}
		else {
			trCouplepays.setVisibility(View.GONE);
		}
		
		
		EditText editTextTax = (EditText) this.findViewById(R.id.editTextTax);
		EditText editTextTip = (EditText) this.findViewById(R.id.editTextTip);
		
		editTextTax.setText(myPreference.getString("TaxPercentage", (String) editTextTax.getHint()));
		editTextTip.setText(myPreference.getString("TipPercentage", (String) editTextTip.getHint()));
		
		
		EditText editTextTip2 = (EditText) this.findViewById(R.id.editTextTip2);
		editTextTip2.setText(myPreference.getString("TipPercentage", (String) editTextTip2.getHint()));
			
		if (viewFlipper.getDisplayedChild() == 0) {
			tipCalcuComplete.calculate();
		}
		else {
			tipCalcuSimple.calculate();
		}
	}
	
	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
			case R.id.buttonCaculate:
			case R.id.buttonCaculate2:
				
				Intent intent = new Intent();  
				intent.setAction(Intent.ACTION_VIEW);  
				intent.addCategory(Intent.CATEGORY_LAUNCHER);  
				  
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
				intent.setComponent(new ComponentName("com.android.calculator2", "com.android.calculator2.Calculator")); 
				startActivity(intent); 
				return;
				
			case R.id.buttonClear:
				tipCalcuComplete.clear();
				tipCalcuSimple.clear();
				return;
				
			case R.id.buttonDone:
				moveTaskToBack(true);
				return;
				
			case R.id.imageButtonTipAdd:
			case R.id.imageButtonTipMinus:
				
				EditText editTextTip = (EditText) this.findViewById(R.id.editTextTip);
				double doubleTip = TipCalcuHelper.ParseDouble(editTextTip.getText().toString());
				
				if (doubleTip % 0.5 == 0)
				{
					if (arg0.getId() == R.id.imageButtonTipAdd)
						doubleTip += 0.5;
					else
						doubleTip -= 0.5;
				}
				else if (doubleTip % 1 < 0.5)
				{
					if (arg0.getId() == R.id.imageButtonTipAdd)
						doubleTip = Math.ceil(doubleTip) - 0.5;
					else
						doubleTip = Math.floor(doubleTip);
				}
				else
				{
					if (arg0.getId() == R.id.imageButtonTipAdd)
						doubleTip = Math.ceil(doubleTip);
					else
						doubleTip = Math.floor(doubleTip) + 0.5;
				}

				if (doubleTip < 0)
					doubleTip = 0;
				
				editTextTip.setText(currencyFormat.format(doubleTip));
				
				tipCalcuComplete.calculate();
				return;
				
			case R.id.imageButtonTipAdd2:
			case R.id.imageButtonTipMinus2:
				
				EditText editTextTip2 = (EditText) this.findViewById(R.id.editTextTip2);
				double doubleTip2 = TipCalcuHelper.ParseDouble(editTextTip2.getText().toString());
				
				if (doubleTip2 % 0.5 == 0)
				{
					if (arg0.getId() == R.id.imageButtonTipAdd2)
						doubleTip2 += 0.5;
					else
						doubleTip2 -= 0.5;
				}
				else if (doubleTip2 % 1 < 0.5)
				{
					if (arg0.getId() == R.id.imageButtonTipAdd2)
						doubleTip2 = Math.ceil(doubleTip2) - 0.5;
					else
						doubleTip2 = Math.floor(doubleTip2);
				}
				else
				{
					if (arg0.getId() == R.id.imageButtonTipAdd2)
						doubleTip2 = Math.ceil(doubleTip2);
					else
						doubleTip2 = Math.floor(doubleTip2) + 0.5;
				}

				if (doubleTip2 < 0)
					doubleTip2 = 0;
				
				editTextTip2.setText(currencyFormat.format(doubleTip2));
				
				tipCalcuSimple.calculate();
				return;
				
			case R.id.imageButtonDividedbyAdd:
			case R.id.imageButtonDividedbyMinus:
				
				int intDividedby = 0;
				
				EditText editTextDividedby = (EditText) this.findViewById(R.id.editTextDividedby);
				
				try {  	
					intDividedby = Integer.parseInt(editTextDividedby.getText().toString()); 
				
				} catch(NumberFormatException nfe) { 
				
					System.out.println("Could not parse " + nfe);
				}  
				
				if (arg0.getId() == R.id.imageButtonDividedbyAdd)
					intDividedby++;
				else
					intDividedby--;
				
				if (intDividedby < 1)
					return;
				
				editTextDividedby.setText(Integer.toString(intDividedby));
				
				if (viewFlipper.getDisplayedChild() == 0)
					tipCalcuComplete.calculate();
				else
					tipCalcuSimple.calculate();
				return;
				
			case R.id.imageButtonEachpaysAdd:				
			case R.id.imageButtonEachpaysMinus:
				if (viewFlipper.getDisplayedChild() == 0)
					tipCalcuComplete.calculate2(arg0.getId());
				else {
					// TODO
				}
							
				return;
				
		}
	}
}